const app = getApp()
Page({
  data: {
    allSelect:"circle",
    num:0,
    count:0
  },
  newadd: function (e) {
    wx.navigateTo({
      url: "/pages/yiguo/address2/address2"
    })
  },
  edit: function (e) {
    wx.navigateTo({
      url: "/pages/yiguo/address3/address3"
    })
  },
  //改变选框状态
  change:function(e){
    var that=this
    //得到下标
    var index = e.currentTarget.dataset.index
    //得到选中状态
    var select = e.currentTarget.dataset.select
    console.log(select);  
    if(select == "circle"){
       var stype="success"
    }else{
       var stype="circle"
    }
    
    //把新的值给新的数组
   var newList= that.data.list
   
   newList[index].select=stype
    //把新的数组传给前台
    that.setData({
      list:newList
    })
  },
  onLoad: function (options) {
    var that = this;
    var ids = app.whereId.uid;
    wx.request({
      url: 'https://www.cakeyouxuan.com/small/my/address',
      data: {
        ids: ids,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"  //post
      },
      success: function (res) {
        that.setData({
          list: res.data.list,
        });
      },
    })
  },
  backme:function(){
    wx.switchTab({
      url: '/pages/yiguo/mine/mine',
    })
  },
  //删除地址
  dels: function (e){
    var id = e.target.dataset.id;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除这个地址吗',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: 'https://www.cakeyouxuan.com/small/my/add_del',
            data: {
              id: id,
            },
            method: "POST",
            header: {
              "Content-Type": "application/x-www-form-urlencoded"  //post
            },
            success: function (res) {
              console.log(res);
              if (res.data.msg == 'T') {
                wx.showToast({
                  title: '删除成功',
                  icon: 'success',
                  duration: 2000,
                  success: function () {
                    setTimeout(function () {
                      //要延时执行的代码
                      that.onLoad();
                    }, 2000) //延迟时间
                  }
                })

              }
            },
          })
        } else if (res.cancel) {

        }
      }
    })
  },
  //修改地址
  edit:function(e){
    var id = e.target.dataset.id;
    wx.navigateTo({
      url: "/pages/yiguo/address3/address3?ids=" + id,
    })
  },

  //点击默认地址
  moren:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.request({
      url: 'https://www.cakeyouxuan.com/small/my/moren',
      data: {
        ids: id,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"  //post
      },
      success: function (res) {
          if (res.data.msg == 'T'){
            wx.showToast({
              title: '设置默认成功',
              icon: 'success',
              duration: 2000,
              success: function () {
                setTimeout(function () {
                  //要延时执行的代码
                  that.onLoad();
                }, 2000) //延迟时间
              }
            })
          }
      },
    })
  },
})